﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;



namespace Mall.Infrastructure.Container
{
    /// <summary>
    /// 容器注入自定义配置
    /// </summary>
    public class ContainerSection : ConfigurationSection
    {
        /// <summary>
        /// Gets the IPItemElement element.
        /// </summary>
        [ConfigurationProperty("ServiceElement")]
        public ServiceElementCollection Services
        {
            get { return (ServiceElementCollection)this["ServiceElement"]; }
        }

        /// <summary>
        /// Gets the IPItemElement element.
        /// </summary>
        [ConfigurationProperty("HostElement")]
        public HostElementCollection Hosts
        {
            get { return (HostElementCollection)this["HostElement"]; }
        }
    }

    [ConfigurationCollection(typeof(ServiceElement), AddItemName = "Service", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class ServiceElementCollection : ConfigurationElementCollection
    {
        public ServiceElement this[int index]
        {
            get { return (ServiceElement)base.BaseGet(index); }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                base.BaseAdd(index, value);
            }
        }

        public new ServiceElement this[string name]
        {
            get { return (ServiceElement)base.BaseGet(name); }
        }


        protected override ConfigurationElement CreateNewElement()
        {
            return new ServiceElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as ServiceElement).Name;
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }

        protected override string ElementName
        {
            get { return "Service"; }
        }

    }


        //自定义配置元素 
    public class ServiceElement  : ConfigurationElement 
    {
        public ServiceElement() { }

            /// <summary>
            /// 接口名称
            /// </summary>
            [ConfigurationProperty("Contract", DefaultValue = "", IsRequired = true)]
            public string Contract
            {

                get { return (string)base["Contract"]; }

                set { base["Contract"] = value; } 

            }


            /// <summary>
            /// 类型(Local：本地加载,Wcf:Wcf加载)
            /// </summary>
            [ConfigurationProperty("Type", DefaultValue = "", IsRequired = true)]
            public string Type 
            {

                get { return (string)base["Type"]; }

                set { base["Type"] = value; } 

            }
             
        /// <summary>
        /// 实例化名称或wcf里client的Name
        /// </summary>
            [ConfigurationProperty("Name", DefaultValue = "", IsRequired = true)]
            public string Name
            {

                get { return (string)base["Name"]; }

                set { base["Name"] = value; }

            }
             
            /// <summary>
            /// 实例化的命名空间
            /// </summary>
            [ConfigurationProperty("Namespace", DefaultValue = "", IsRequired = false)]
            public string Namespace
            {

                get { return (string)base["Namespace"]; }

                set { base["Namespace"] = value; }

            } 
    }



    [ConfigurationCollection(typeof(HostElement), AddItemName = "Host", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class HostElementCollection : ConfigurationElementCollection
    {
        public HostElement this[int index]
        {
            get { return (HostElement)base.BaseGet(index); }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                base.BaseAdd(index, value);
            }
        }

        public new HostElement this[string name]
        {
            get { return (HostElement)base.BaseGet(name); }
        }


        protected override ConfigurationElement CreateNewElement()
        {
            return new HostElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as HostElement).Contract;
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }

        protected override string ElementName
        {
            get { return "Host"; }
        }

    }

    //自定义配置元素 
    public class HostElement : ConfigurationElement
    {
        public HostElement() { }

        /// <summary>
        /// 接口名称
        /// </summary>
        [ConfigurationProperty("Contract", DefaultValue = "", IsRequired = true)]
        public string Contract
        {

            get { return (string)base["Contract"]; }

            set { base["Contract"] = value; }

        }


        /// <summary>
        /// 实例化的命名空间
        /// </summary>
        [ConfigurationProperty("Namespace", DefaultValue = "", IsRequired = true)]
        public string Namespace
        {

            get { return (string)base["Namespace"]; }

            set { base["Namespace"] = value; }

        } 
    }
}
